package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @PostMapping
    public Result addBusiness(@RequestBody Business business) {
            log.info("新增商机：{}", business);
            business.setStatus(0);
            business.setCreateTime(LocalDateTime.now());
            business.setUpdateTime(LocalDateTime.now());
            businessService.save(business);
            return Result.success();
    }
    @GetMapping
    public Result selectBusiness(BusinessDto businessDto) {
        log.info("查询商机：{}", businessDto);
        PageResult<Business> pageResult = businessService.listBusiness(businessDto);
        return Result.success(pageResult);
    }
    //分配商机
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId){
        log.info("分配商机：{}", businessId);
        Business business = new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }
    //根据ID查询商机
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id){
        log.info("查询id{}",id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }
    //跟进商机
    @PutMapping
    public Result traceBusiness(@RequestBody Business business){
        log.info("跟进商机{}",business);
        businessService.updateBusinessById(business);
        return Result.success();
    }
    //踢回公海
    @PutMapping("/back/{id}")
    public Result backid(@PathVariable Integer id){
        log.info("踢回公海{}",id);
        businessService.backToSea(id);
        return Result.success();
    }
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("将指定商机转为客户{}", id);
        businessService.toCustomer(id);
        return Result.success();
    }
    @GetMapping("/pool")
    public Result selectPoolBusiness(BusinessDto businessDto) {
        log.info("查询公海商机");
        PageResult<Business> pageResult = businessService.poolBusiness(businessDto);
        return Result.success(pageResult);
    }
}
